define(['jquery',"jweixin"],function($, wx){
	var $this =	{
		init : function(obj) {
			obj = obj || {};
			this.imageLocalId = [];
			this.imageServerId = [];
			this.uploadIndex = 0;
			this.baseId = "#" + obj.id;
			this.title = obj.title;
			this.status = "";
			this.config(obj);
			this.initUi();
		}, // end init
		config : function(obj) {
			obj = obj || {};
			wx.config(obj.config);// 初始化微信信息
			wx.ready(function() {
				if(obj.success) obj.success();
			});// end wx.ready
			wx.error(function(){
				$.alert('微信认证失败，请重新打开页面');
				if(obj.error) obj.error();
			});// end wx.error
		},// end config
		countImage : function() {
			 $($this.baseId).find(".imgcount").html($this.imageServerId.length + "/" + $this.imageLocalId.length);
		}, // end countImage
		addLocalImage : function(localId) {//展示选中的图片
        	//weui_uploader_status <div class="weui_uploader_status_content">80%</div>
        	$($this.baseId).find(".weui_uploader_files").append('<li class="weui_uploader_file" localId="'+localId+'" serverId="" style="background-image:url('+localId+');position:relative;"><span class="glyphicon glyphicon-trash" style="position:absolute;top:0px;right:0px;" aria-hidden="true"></span></li>');
        	$($this.baseId).find(".weui_uploader_files .weui_uploader_file[localId='"+localId+"']").click(function() {
        		$this.previewImage(localId, $this.imageLocalId);
	        }).find(".glyphicon-trash").click(function() {
	        	$this.deleteLocalImage($(this).parent().attr("localId"));
	        	$this.deleteServerImage($(this).parent().attr("serverId"));
				$(this).parent().remove();
    		});
        	$this.imageLocalId.push(localId);
        	$this.countImage();
		}, // end 
		deleteLocalImage : function(n) {
			var tmp = $this.imageLocalId;
			$this.imageLocalId.splice(0, $this.imageLocalId.length);//清空数组 
			for(var index in tmp){
				if(tmp[index] != n) {
					$this.imageLocalId.push(tmp[index]);
				}
	    	}
			$this.countImage();
		},// end deleteLocalImage
		addServerImage : function(localId, serverId) {
			$($this.baseId).find(".weui_uploader_files .weui_uploader_file[localId='"+localId+"']").attr("serverId", serverId);
        	$this.imageServerId.push(serverId);
        	$this.countImage();
		},// end addServerImage
		deleteServerImage : function(n) {
			var tmp = $this.imageServerId;
			$this.imageServerId.splice(0, $this.imageServerId.length);//清空数组 
			for(var index in tmp){
				if(tmp[index] != n) {
					$this.imageServerId.push(tmp[index]);
				}
	    	}
			$this.countImage();
		},// end deleteServerImage
		chooseImage : function(obj) {//拍照或从手机相册中选图接口
			obj = obj || {};
			if(obj.config) {
				$this.config({
					config : obj.config,
					success : function(){
						obj.config = null;//避免死循环
						$this.chooseImage(obj); 
					},
				});// 初始化微信信息
			} else {
				wx.chooseImage({
					count : ((obj.max||9) - $($this.baseId).find(".weui_uploader_file").length), // 默认9
					sizeType: [obj.original||'original', obj.compressed||'compressed'], // 可以指定是原图还是压缩图，默认二者都有
				    sourceType : [obj.album||'album', obj.camera||'camera'], // 可以指定来源是相册还是相机，默认二者都有
				    success: function (res) {//JSON.stringify(res)
				        $(res.localIds).each(function(i, localId) {// 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
				        	$this.addLocalImage(localId);
				        });
				    }
				});// end wx.chooseImage
			}
		}, // end chooseImage
		previewImage : function(obj) {// 图片预览
			obj = obj || {};
			if(obj.config) {
				$this.config({
					config : obj.config,
					success : function(){
						obj.config = null;//避免死循环
						$this.previewImage(obj); 
					},
				});// 初始化微信信息
			} else {
				wx.previewImage({
				    current: obj.current, // 当前显示图片的http链接
				    urls: obj.urls//[] // 需要预览的图片http链接列表
				});
			}
		}, // end previewImage
		uploadImage : function(obj) {//图片上传接口
			obj = obj || {};
			if(obj.config) {
				$this.config({
					config : obj.config,
					success : function(){
						obj.config = null;//避免死循环
						$this.uploadImage(obj); 
					},
				});// 初始化微信信息
			} else {
				if($this.imageLocalId == null || $this.imageLocalId.length == 0) {
					$.alert("请选择照片");
					return ;
				} 
				var localId = $this.imageLocalId[$this.uploadIndex];
				if (localId.indexOf("wxlocalresource") != -1) {//解决IOS无法上传的坑
					localId = localId.replace("wxlocalresource", "wxLocalResource");
				}
				if($this.uploadIndex == 0) {
					$.showLoading("正在提交...");
				}
				wx.uploadImage({
				    localId : localId, // 需要上传的图片的本地ID，由chooseImage接口获得
				    isShowProgressTips : (obj.isShowProgressTips||0), // 默认为1，显示进度提示
				    success: function (res) {//JSON.stringify(res)
				        $this.addServerImage(localId, res.serverId); // 返回图片的服务器端ID
				    	if(++$this.uploadIndex < $this.imageLocalId.length) {
				    		obj.config = null;//避免死循环
				    		$this.uploadImage(obj);
				    	} else {
				    		--$this.uploadIndex;
					    	$.hideLoading();
					    	if(obj.success) obj.success();
				    	}
				    },
				    error : function(res) {
						$.alert('上传失败请重试');
				    	$.hideLoading();
				    	if(obj.error) obj.error();
				    }
				}); // end wx.uploadImage
			}
		}, // end uploadImage
		downloadImage : function(obj) {//图片下载接口
			obj = obj || {};
			if(obj.config) {
				$this.config({
					config : obj.config,
					success : function(){
						obj.config = null;//避免死循环
						$this.downloadImage(obj); 
					},
				});// 初始化微信信息
			} else {
				wx.downloadImage({
				    serverId: serverId, // 需要下载的图片的服务器端ID，由uploadImage接口获得
				    isShowProgressTips: (obj.isShowProgressTips||1), // 默认为1，显示进度提示
				    success: function (res) {//JSON.stringify(res)
				        var localId = res.localId; // 返回图片下载后的本地ID
				        if (obj.success) obj.success(res);
				    },
				    error : function(res) {
				        if (obj.error) obj.error(res);
				    }
				});// end wx.downloadImage
			}
		}, // end downloadImage
		initUi : function() {
			var html = '<div class="weui_cells weui_cells_form">'+
			        '<div class="weui_cell">'+
			            '<div class="weui_cell_bd weui_cell_primary">'+
			                '<div class="weui_uploader">'+
			                    '<div class="weui_uploader_hd weui_cell">'+
			                        '<div class="weui_cell_bd weui_cell_primary">'+$this.title+'</div>'+
			                        '<div class="weui_cell_ft imgcount">0/0</div>'+
			                    '</div>'+
			                    '<div class="weui_uploader_bd">'+
			                        '<ul class="weui_uploader_files">'+
			                            '<!-- '+
			                            '<li class="weui_uploader_file" style="background-image:url(http://shp.qpic.cn/weixinsrc_pic/pScBR7sbqjOBJomcuvVJ6iacVrbMJaoJZkFUIq4nzQZUIqzTKziam7ibg/)"></li>'+
			                            '<li class="weui_uploader_file weui_uploader_status" style="background-image:url(http://shp.qpic.cn/weixinsrc_pic/pScBR7sbqjOBJomcuvVJ6iacVrbMJaoJZkFUIq4nzQZUIqzTKziam7ibg/)">'+
			                                '<div class="weui_uploader_status_content">'+
			                                    '<i class="weui_icon_warn"></i>'+
			                                '</div>'+
			                            '</li>'+
			                            '<li class="weui_uploader_file weui_uploader_status" style="background-image:url(http://shp.qpic.cn/weixinsrc_pic/pScBR7sbqjOBJomcuvVJ6iacVrbMJaoJZkFUIq4nzQZUIqzTKziam7ibg/)">'+
			                                '<div class="weui_uploader_status_content">50%</div>'+
			                            '</li>'+
			                            '-->'+
			                        '</ul>'+
			                        '<div class="weui_uploader_input_wrp">'+
			                            '<input class="weui_uploader_input" type="file" accept="image/*" multiple/>'+
			                        '</div>'+
			                    '</div>'+
			                '</div>'+
			            '</div>'+
			        '</div>'+
				'</div>';
			$($this.baseId).html(html);
			$($this.baseId).find(".weui_uploader_input").click(function() {
				$this.chooseImage();
				return false;
			});
		}, // end createUl
	}// end 
	return $this;
});